﻿Imports TBCC.UserManage
Public Class ImageControlConfigForm

#Region "构造函数"

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Public Sub New(ByVal item As ProjectImageItem, ByVal pi As ProjectItem)
        MyClass.New()

        _pii = item
        _pi = pi
    End Sub

#End Region

    Private _pii As ProjectImageItem = Nothing
    Private _pi As ProjectItem = Nothing

    '控件拖动控制实例
    Private pb As New PickBox

    '窗体加载完成标志
    Private _bLoad As Boolean = False

    Private _isOfflineMode As Boolean

    Private Sub ImageControlConfigForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadImage()
        LoadControls()
        Me.PictureBox1.SendToBack()

        LoadCtrlTypes_Online()

        Me.btnAdd.Enabled = False
        ''btnSave.Enabled = False
        Me.btnRefresh.Enabled = False
        Me.btnRefresh.Visible = False
        _bLoad = True
        Dim gp As System.Security.Principal.GenericPrincipal
        gp = My.User.CurrentPrincipal
        If gp.IsInRole(RoleClass.GetRolename(RoleType.MiddleLevel)) Then
            Me.btnOfflineData.Enabled = False
        End If
    End Sub

    Private Sub loadImage()
        If System.IO.File.Exists(_pii.imagePath) Then
            Me.PictureBox1.Image = Image.FromFile(_pii.imagePath)
        ElseIf System.IO.File.Exists(_pii.imageoppPath) Then
            _pii.imagePath = _pii.imageoppPath
            Me.PictureBox1.Image = Image.FromFile(_pii.imagePath)

        End If
    End Sub

    Private Sub LoadControls()
        Me.Text = String.Format("图层配置 - {0}", _pii.imageName)

        For Each item As ImgControlItem In ImgControlService.GetControls(_pii.projectID, _pii.ImageID)
            Dim ctl As Control
            ctl = CreateImageControl(item)

            If ctl IsNot Nothing Then
                Me.Panel1.Controls.Add(ctl)
                pb.WireControl(ctl)
            End If
        Next

    End Sub

    Private Function CreateImageControl(ByVal item As ImgControlItem) As Control
        Dim ctl As Control

        '' =============================
        '' lidalong
        '' 2010-09-21
        '' for: 支持库门状态动画
        If (item.IsDoorStatus) Then
            ctl = New CtlAnimator
            CType(ctl, CtlAnimator).IsDesign = True
            CType(ctl, CtlAnimator).ImgControlItem = item

            AddHandler CType(ctl, CtlAnimator).DeleteEvent, AddressOf OnDelControl
        Else
            ctl = New ImgControl(item.DataType)

            CType(ctl, ImgControl).ici = item
            CType(ctl, ImgControl).IsDesign = True
            AddHandler CType(ctl, ImgControl).DeleteEvent, AddressOf OnDelControl

        End If

        If ctl Is Nothing Then
            Return Nothing
        End If


        Dim iWidth As Integer = Me.PictureBox1.Width
        Dim iHeight As Integer = Me.PictureBox1.Height

        ctl.Location = New Point((item.cLeft / 1024.0) * iWidth, (item.cTop / 768.0) * iHeight)
        ctl.Size = New Size(item.cWidth, item.cHeight) '宽度和高度不变
        'ctl.Size = New Size((item.cWidth / 1024.0) * iWidth, (item.cHeight / 768.0) * iHeight)

        Return ctl
    End Function


    Private Sub OnDelControl(ByVal sender As Object, ByVal e As EventArgs)
        btnDelete.PerformClick()
    End Sub


#Region "加载控件项目配置数据"
    Private Sub LoadCtrlTypes_Online()

        Me.TreeView1.Nodes.Clear()

        Dim tn As TreeNode
        Dim tn1 As TreeNode

        Dim rts As New RefTypeService(_pi)

        For Each rti As RefTypeItem In rts.GeRefGroupList() '增加冷库名称
            tn = Me.TreeView1.Nodes.Add(rti.RefName)
            tn.Tag = rti

            Dim apts As New AiPortTypeService(rti)

            For Each apti As AiPortTypeItem In apts.GetAiGroupList() '增加该冷库的温湿度名称
                If Not String.IsNullOrEmpty(apti.PortName) Then
                    tn1 = tn.Nodes.Add(apti.PortName)
                    tn1.Tag = apti
                End If
            Next

            Dim diopts As New DiDoPortTypeService(rti)
            For Each diopti As DiDoPortTypeItem In diopts.GetDioGroupList() '增加该冷库的报警信息
               

                If diopti.LostPowerPort >= 1 Then '缺项报警配置
                    tn1 = tn.Nodes.Add("断电预警")
                    tn1.Tag = diopti
                End If
                If diopti.GeneratorPreAlarm_Nu >= 1 Then '发电机启动状态输入
                    tn1 = tn.Nodes.Add("发电机故障预警")
                    tn1.Tag = diopti
                End If

                If diopti.DoorStatusPort >= 1 Then '库门预警配置
                    tn1 = tn.Nodes.Add("库门1预警")
                    tn1.Tag = diopti
                End If
                If diopti.SoundAlarmPort >= 1 Then '声光报警配置
                    tn1 = tn.Nodes.Add("声光报警")
                    tn1.Tag = diopti
                End If
                If diopti.RefAlarmPort >= 1 Then '冷库报警状态
                    tn1 = tn.Nodes.Add("冷库报警")
                    tn1.Tag = diopti
                End If
           
                If diopti.DoorStatusPort2 >= 1 Then   '增加库门2预警
                    tn1 = tn.Nodes.Add("库门2预警")
                    tn1.Tag = diopti

                End If
              
                If diopti.JointActionOpen_Output_Nu >= 1 Then   '增加强制制冷开启状态
                    tn1 = tn.Nodes.Add("强制制冷开启状态")
                    tn1.Tag = diopti

                End If

                If diopti.JointActionClose_Output_Nu >= 1 Then   '增加强制制冷关闭状态
                    tn1 = tn.Nodes.Add("强制制冷关闭状态")
                    tn1.Tag = diopti

                End If

                If diopti.JointAction_Output_Nu >= 1 Then   '增加联动报警
                    tn1 = tn.Nodes.Add("联动报警")
                    tn1.Tag = diopti

                End If

                If diopti.DefrostStatus_Input_Nu >= 1 Then   '增加除霜状态
                    tn1 = tn.Nodes.Add("除霜状态")
                    tn1.Tag = diopti

                End If

                If diopti.Cooling_Input_Nu >= 1 Then   '增加制冷状态
                    tn1 = tn.Nodes.Add("制冷状态")
                    tn1.Tag = diopti

                End If

                If diopti.CompressorSetFailure_Input_Nu >= 1 Then   '增加压缩机组预警
                    tn1 = tn.Nodes.Add("机组故障预警")
                    tn1.Tag = diopti

                End If

               

                If diopti.DefrostTemp_Input_Nu >= 1 Then   '增加除霜预警
                    tn1 = tn.Nodes.Add("除霜预警")
                    tn1.Tag = diopti

                End If

                If diopti.CompressorFailure_Input_Nu >= 1 Then   '增加压缩机预警
                    tn1 = tn.Nodes.Add("压缩机故障预警")
                    tn1.Tag = diopti

                End If

                If diopti.Compressor_Input_Nu >= 1 Then   '增加压缩机状态
                    tn1 = tn.Nodes.Add("压缩机运行状态")
                    tn1.Tag = diopti

                End If


                If diopti.DefrostTemp_Input_Nu >= 1 Then   '除霜温度
                    tn1 = tn.Nodes.Add("除霜温度")
                    tn1.Tag = diopti

                End If
                
                
            Next

        Next

        ''add for bssh begin*****
        'tn = Me.TreeView1.Nodes.Add("原有系统")

        'for i as Integer  =0 to 8
        '    tn.Nodes.Add("T" & i)
        'Next

        'For i As Integer = 0 To 8
        '    tn.Nodes.Add("H" & i)
        'Next
        ''add for bssh end*****

        Me.TreeView1.ExpandAll()
    End Sub

    Private Sub LoadCtrlTypes_Offline()

        Me.TreeView1.Nodes.Clear()

        Dim tn As TreeNode
        Dim tn1 As TreeNode

        Dim rts As New RefTypeService(_pi)

        For Each rti As RefTypeItem In rts.GeRefGroupList() '增加冷库名称
            tn = Me.TreeView1.Nodes.Add(rti.RefName)
            tn.Tag = rti

            Dim ipts As New IOPortTypeService(rti)

            For Each ipi As IOPortTypeItem In ipts.GetAiGroupList() '增加该冷库的温湿度名称
                If Not String.IsNullOrEmpty(ipi.PortName) Then
                    tn1 = tn.Nodes.Add(ipi.PortName)
                    Dim apti As New AiPortTypeItem()
                    apti.ProjectID = ipi.ProjectID
                    apti.NetId = ipi.NetId
                    apti.RefId = ipi.RefId
                    apti.SeqPortNo = ipi.SeqPortNo
                    apti.PortNo = ipi.PortNo
                    apti.PortName = ipi.PortName
                    apti.DataType = ipi.DataType
                    apti.PortNoType = ipi.PortNoType
                    tn1.Tag = apti
                End If
            Next

            Dim dpti As New DiDoPortTypeItem()
            dpti.ProjectID = rti.ProjectID
            dpti.NetId = rti.NetId
            dpti.RefId = rti.RefId
            Dim devtype As Integer
            'devtype = DevTypeService.GetdevType(rti.ProjectID, rti.NetId)

            'Select Case devtype
            '    Case 1
            '        If rti.NetId = 1 AndAlso rti.RefId = 1 Then '只允许1号网络设备的1号冷库配置"停电报警输入端口号"和"声光报警输出端口"
            '            For Each ipi As IOPortTypeItem In ipts.GetDiGroupList() '增加该冷库的报警信息
            '                Select Case ipi.SeqPortNo
            '                    Case 1
            '                        tn1 = tn.Nodes.Add("断电预警")
            '                        dpti.LostPowerPort = ipi.PortNo
            '                        tn1.Tag = dpti
            '                    Case 2
            '                        tn1 = tn.Nodes.Add("库门1预警")
            '                        dpti.DoorStatusPort = ipi.PortNo
            '                        tn1.Tag = dpti
            '                    Case 5
            '                        tn1 = tn.Nodes.Add("库门2预警")
            '                        dpti.DoorStatusPort2 = ipi.PortNo
            '                        tn1.Tag = dpti

            '                End Select
            '            Next

            '            For Each ipi As IOPortTypeItem In ipts.GetDiGroupListCold() '增加该冷库的报警信息

            '                Select Case ipi.SeqPortNo

            '                    Case 10
            '                        tn1 = tn.Nodes.Add("除霜状态")
            '                        dpti.DefrostStatus_Input_Nu = ipi.PortNo
            '                        tn1.Tag = dpti

            '                End Select

            '            Next

            '            For Each ipi As IOPortTypeItem In ipts.GetDoGroupList() '增加该冷库的报警信息
            '                Select Case ipi.SeqPortNo
            '                    Case 3
            '                        tn1 = tn.Nodes.Add("冷库报警")
            '                        dpti.RefAlarmPort = ipi.PortNo
            '                        tn1.Tag = dpti
            '                    Case 4
            '                        tn1 = tn.Nodes.Add("声光报警")
            '                        dpti.SoundAlarmPort = ipi.PortNo
            '                        tn1.Tag = dpti
            '                End Select
            '            Next


            '            For Each ipi As IOPortTypeItem In ipts.GetDoGroupListCold() '增加该冷库的报警信息

            '                Select Case ipi.SeqPortNo
            '                    Case 7
            '                        tn1 = tn.Nodes.Add("强制制冷开启状态")
            '                        dpti.JointActionOpen_Output_Nu = ipi.PortNo
            '                        tn1.Tag = dpti
            '                    Case 8
            '                        tn1 = tn.Nodes.Add("强制制冷关闭状态")
            '                        dpti.JointActionClose_Output_Nu = ipi.PortNo
            '                        tn1.Tag = dpti
            '                    Case 9
            '                        tn1 = tn.Nodes.Add("联动报警")
            '                        dpti.JointAction_Output_Nu = ipi.PortNo
            '                        tn1.Tag = dpti
            '                        'Case 10
            '                        '    tn1 = tn.Nodes.Add("除霜状态")
            '                        '    dpti.DefrostStatus_Input_Nu = ipi.PortNo
            '                        '    tn1.Tag = dpti

            '                End Select

            '            Next


            '        Else
            '            For Each ipi As IOPortTypeItem In ipts.GetDiGroupList() '增加该冷库的报警信息
            '                Select Case ipi.SeqPortNo
            '                    Case 1
            '                        tn1 = tn.Nodes.Add("库门1预警")
            '                        dpti.DoorStatusPort = ipi.PortNo
            '                        tn1.Tag = dpti
            '                    Case 3
            '                        tn1 = tn.Nodes.Add("库门2预警")
            '                        dpti.DoorStatusPort2 = ipi.PortNo
            '                        tn1.Tag = dpti
            '                End Select
            '            Next

            '            For Each ipi As IOPortTypeItem In ipts.GetDiGroupListCold() '增加该冷库的报警信息

            '                Select Case ipi.SeqPortNo

            '                    Case 10
            '                        tn1 = tn.Nodes.Add("除霜状态")
            '                        dpti.DefrostStatus_Input_Nu = ipi.PortNo
            '                        tn1.Tag = dpti

            '                End Select

            '            Next

            '            For Each ipi As IOPortTypeItem In ipts.GetDoGroupList() '增加该冷库的报警信息
            '                Select Case ipi.SeqPortNo
            '                    Case 2
            '                        tn1 = tn.Nodes.Add("冷库报警")
            '                        dpti.RefAlarmPort = ipi.PortNo
            '                        tn1.Tag = dpti
            '                End Select
            '            Next

            '            For Each ipi As IOPortTypeItem In ipts.GetDoGroupListCold() '增加该冷库的报警信息

            '                Select Case ipi.SeqPortNo
            '                    Case 7
            '                        tn1 = tn.Nodes.Add("强制制冷开启状态")
            '                        dpti.JointActionOpen_Output_Nu = ipi.PortNo
            '                        tn1.Tag = dpti
            '                    Case 8
            '                        tn1 = tn.Nodes.Add("强制制冷关闭状态")
            '                        dpti.JointActionClose_Output_Nu = ipi.PortNo
            '                        tn1.Tag = dpti
            '                    Case 9
            '                        tn1 = tn.Nodes.Add("联动报警")
            '                        dpti.JointAction_Output_Nu = ipi.PortNo
            '                        tn1.Tag = dpti
            '                        'Case 10
            '                        '    tn1 = tn.Nodes.Add("除霜状态")
            '                        '    dpti.DefrostStatus_Input_Nu = ipi.PortNo
            '                        '    tn1.Tag = dpti

            '                End Select

            '            Next


            '        End If



            '  Case 16
            If rti.NetId = 100 AndAlso rti.RefId = 1 Then '只允许1号网络设备的1号冷库配置"停电报警输入端口号"和"声光报警输出端口"
                For Each ipi As IOPortTypeItem In ipts.GetDiGroupList() '增加该冷库的报警信息
                    Select Case ipi.SeqPortNo
                        Case 1
                            tn1 = tn.Nodes.Add("断电预警")
                            dpti.LostPowerPort = ipi.PortNo
                            tn1.Tag = dpti
                        Case 2
                            tn1 = tn.Nodes.Add("发电机故障预警")
                            dpti.DoorStatusPort2 = ipi.PortNo
                            tn1.Tag = dpti
                        Case 3
                            tn1 = tn.Nodes.Add("库门1预警")
                            dpti.DoorStatusPort = ipi.PortNo
                            tn1.Tag = dpti
                        Case 4
                            tn1 = tn.Nodes.Add("库门2预警")
                            dpti.DoorStatusPort2 = ipi.PortNo
                            tn1.Tag = dpti


                    End Select
                Next


                For Each ipi As IOPortTypeItem In ipts.GetDoGroupList() '增加该冷库的报警信息
                    Select Case ipi.SeqPortNo
                        Case 5
                            tn1 = tn.Nodes.Add("声光报警")
                            dpti.SoundAlarmPort = ipi.PortNo
                            tn1.Tag = dpti

                        Case 6

                            tn1 = tn.Nodes.Add("冷库报警")
                            dpti.RefAlarmPort = ipi.PortNo
                            tn1.Tag = dpti


                    End Select
                Next

               

            ElseIf rti.RefId <= 4 Then

                For Each ipi As IOPortTypeItem In ipts.GetDiGroupList() '增加该冷库的报警信息
                    Select Case ipi.SeqPortNo
                        Case 1
                            tn1 = tn.Nodes.Add("库门1预警")
                            dpti.DoorStatusPort = ipi.PortNo
                            tn1.Tag = dpti
                        Case 2
                            tn1 = tn.Nodes.Add("库门2预警")
                            dpti.DoorStatusPort2 = ipi.PortNo
                            tn1.Tag = dpti

                    End Select
                Next

                For Each ipi As IOPortTypeItem In ipts.GetDoGroupList() '增加该冷库的报警信息
                    Select Case ipi.SeqPortNo
                        Case 3
                            tn1 = tn.Nodes.Add("冷库报警")
                            dpti.RefAlarmPort = ipi.PortNo
                            tn1.Tag = dpti

                    End Select
                Next

            Else

                For Each ipi As IOPortTypeItem In ipts.GetDoGroupList() '增加该冷库的报警信息
                    Select Case ipi.SeqPortNo
                        Case 1
                            tn1 = tn.Nodes.Add("冷库报警")
                            dpti.RefAlarmPort = ipi.PortNo
                            tn1.Tag = dpti

                    End Select
                Next

               

            End If




            For Each ipi As IOPortTypeItem In ipts.GetDoGroupListCold() '增加该冷库的报警信息

                Select Case ipi.SeqPortNo
                    Case 7
                        tn1 = tn.Nodes.Add("强制制冷开启状态")
                        dpti.JointActionOpen_Output_Nu = ipi.PortNo
                        tn1.Tag = dpti
                    Case 8
                        tn1 = tn.Nodes.Add("强制制冷关闭状态")
                        dpti.JointActionClose_Output_Nu = ipi.PortNo
                        tn1.Tag = dpti
                    Case 9
                        tn1 = tn.Nodes.Add("联动报警")
                        dpti.JointAction_Output_Nu = ipi.PortNo
                        tn1.Tag = dpti

                End Select

            Next


            For Each ipi As IOPortTypeItem In ipts.GetDiGroupListCold() '增加该冷库的报警信息

                Select Case ipi.SeqPortNo

                    Case 10

                        tn1 = tn.Nodes.Add("压缩机运行状态")
                        dpti.Compressor_Input_Nu = ipi.PortNo
                        tn1.Tag = dpti

                    Case 11

                        tn1 = tn.Nodes.Add("除霜状态")
                        dpti.DefrostStatus_Input_Nu = ipi.PortNo
                        tn1.Tag = dpti

                    Case 12
                        tn1 = tn.Nodes.Add("制冷状态")
                        dpti.Cooling_Input_Nu = ipi.PortNo
                        tn1.Tag = dpti

                    Case 13
                        tn1 = tn.Nodes.Add("机组故障预警")
                        dpti.CompressorSetFailure_Input_Nu = ipi.PortNo
                        tn1.Tag = dpti
                        'tn1 = tn.Nodes.Add("除霜预警")
                        'dpti.DefrostTemp_Input_Nu = ipi.PortNo
                        'tn1.Tag = dpti
                    Case 14
                        tn1 = tn.Nodes.Add("压缩机故障预警")
                        dpti.CompressorFailure_Input_Nu = ipi.PortNo
                        tn1.Tag = dpti
                    Case 15
                        tn1 = tn.Nodes.Add("除霜温度")
                        dpti.DefrostTemp_Input_Nu = ipi.PortNo
                        tn1.Tag = dpti


                        'If diopti.Compressor_Input_Nu >= 1 Then   '增加压缩机状态
                        '    tn1 = tn.Nodes.Add("压缩机运行状态")
                        '    tn1.Tag = diopti

                        'End If


                        'If diopti.DefrostTemp_Input_Nu >= 1 Then   '除霜温度
                        '    tn1 = tn.Nodes.Add("除霜温度")
                        '    tn1.Tag = diopti

                        'End If

                End Select

            Next




        Next

        ''add for bssh begin*****
        'tn = Me.TreeView1.Nodes.Add("原有系统")

        'For i As Integer = 0 To 8
        '    tn.Nodes.Add("T" & i)
        'Next

        'For i As Integer = 0 To 8
        '    tn.Nodes.Add("H" & i)
        'Next
        ''add for bssh end*****

        Me.TreeView1.ExpandAll()
    End Sub
#End Region


    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim tic As New TranstrationItem()

        Dim strSql As String

        strSql = "Delete From tbccImageControls Where projectID=?p0 And ImageID=?p1"

        Dim p0(1) As MySql.Data.MySqlClient.MySqlParameter
        p0(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p0(0).ParameterName = "p0" : p0(0).Value = _pii.projectID

        p0(1) = New MySql.Data.MySqlClient.MySqlParameter()
        p0(1).ParameterName = "p1" : p0(1).Value = _pii.ImageID

        tic.Add(strSql, p0)

        Dim iTemp As Integer = 0
        '' ==========================
        '' lidlaong
        '' 2010-09-21
        '' for: 支持库门状态
        For Each ctl As Control In Me.Panel1.Controls
            Dim item As ImgControlItem = Nothing

            If TypeOf ctl Is ImgControl Then
                item = CType(ctl, ImgControl).ici
            ElseIf (TypeOf ctl Is CtlAnimator) Then
                item = CType(ctl, CtlAnimator).ImgControlItem
            Else
                Continue For
            End If

            If (item IsNot Nothing) Then
                strSql = "Insert Into tbccImageControls(ProjectID,Netid,Refid,DataType,PortNo,ImageID,TitleMsg,TipMsg,UnitMsg,cLeft,cTop,cWidth,cHeight) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12)"

                strSql &= "-- " & iTemp
                iTemp += 1

                Dim p(12) As MySql.Data.MySqlClient.MySqlParameter

                For i As Integer = 0 To p.GetUpperBound(0)
                    p(i) = New MySql.Data.MySqlClient.MySqlParameter()
                    p(i).ParameterName = "p" + i.ToString
                Next

                p(0).Value = item.ProjectID
                p(1).Value = item.Netid
                p(2).Value = item.Refid
                '' ==========================
                '' lidlaong
                '' 2011-01-5
                '' for: 支持库门状态
                If TypeOf ctl Is ImgControl Then
                    p(3).Value = CType(ctl, ImgControl).ImgControlType
                ElseIf (TypeOf ctl Is CtlAnimator) Then
                    p(3).Value = item.DataType
                End If
                p(4).Value = item.PortNo
                p(5).Value = item.ImageID
                p(6).Value = item.TitleMsg
                p(7).Value = item.TipMsg
                p(8).Value = item.UnitMsg
                p(9).Value = CInt((ctl.Left * 1.0 / Me.PictureBox1.Width) * 1024)
                p(10).Value = CInt((ctl.Top * 1.0 / Me.PictureBox1.Height) * 768)
                p(11).Value = ctl.Width
                p(12).Value = ctl.Height

                tic.Add(strSql, p)

            End If
        Next

        If SqlHelper.ExecuteTranstration(tic) Then
            msg.Show("保存成功。")

            Dim strMsg As String
            strMsg = String.Format("修改{0}工程－{1}图层成功", _pi.ProjectName, _pii.imageName)
            'Log.log(tbccLogOperateType.ModifyProject, strMsg)

            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            msg.ShowError("保存失败。")

            Dim strMsg As String
            strMsg = String.Format("修改{0}工程－{1}图层失败", _pi.ProjectName, _pii.imageName)
            'Log.log(tbccLogOperateType.ModifyProject, strMsg)

            Exit Sub
        End If
    End Sub


    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim n As TreeNode
        n = Me.TreeView1.SelectedNode

        If n Is Nothing Then
            '没有选择树节点
            Exit Sub
        End If

        If TypeOf n.Tag Is RefTypeItem Then   '选择冷库名称
            '选中的节点是分类，不是具体的组件
            Exit Sub
        End If

        ''add for bssh begin
        'If String.Compare(n.Name, "原有系统") = 0 Then
        '    Exit Sub
        'End If
        ''add for bssh end

        Dim ici As ImgControlItem

        If TypeOf n.Tag Is AiPortTypeItem Then ' 选择AI端口数据
            ici = New ImgControlItem(CType(n.Tag, AiPortTypeItem), _pii.ImageID, n.Parent.Text)
        Else
            'ElseIf TypeOf n.Tag Is DiDoPortTypeItem Then ' 选择DI/DO报警数据
            ici = New ImgControlItem(CType(n.Tag, DiDoPortTypeItem), _pii.ImageID, n.Text, n.Parent.Text)
        End If

        Dim ctl As Control = New ImgControl(ici.DataType)

        If ctl Is Nothing Then
            Exit Sub
        End If

        CType(ctl, ImgControl).IsDesign = True
        CType(ctl, ImgControl).ici = ici

        AddHandler CType(ctl, ImgControl).DeleteEvent, AddressOf OnDelControl
        CType(ctl, ImgControl).InitFont()
        ctl.Location = New Point(150, 150)

        ''============================
        '' lidalong
        '' 2010-09-21
        '' FOR 支持库门状态
        If (ici.PortNo = 4 Or ici.PortNo = 5) AndAlso ici.DataType = 3 Then
            Dim imgControl As ImgControlItem = ici
            Dim ctl1 As CtlAnimator = New CtlAnimator()
            ctl1.ImgControlItem = imgControl
            ctl1.IsDesign = True
            AddHandler ctl1.DeleteEvent, AddressOf OnDelControl
            ctl1.Size = New Size(30, 30)
            ctl1.Location = New Point(150, 180)

            Me.Panel1.Controls.Add(ctl1)

            pb.WireControl(ctl1)

            ctl1.BringToFront()

            Dim ctl2 = New ImgControl(5)        '' 表示为库门 开启或关闭状态
            CType(ctl2, ImgControl).IsDesign = True
            CType(ctl2, ImgControl).ici = ici

            AddHandler CType(ctl2, ImgControl).DeleteEvent, AddressOf OnDelControl
            CType(ctl2, ImgControl).InitFont()
            ctl2.Location = New Point(150, 120)
            Me.Panel1.Controls.Add(ctl2)
            pb.WireControl(ctl2)
            ctl2.BringToFront()
        End If

        If (ici.PortNo = 9) AndAlso ici.DataType = 3 Then

            Dim ctl2 = New ImgControl(6)        '' 除霜 预警
            CType(ctl2, ImgControl).IsDesign = True
            CType(ctl2, ImgControl).ici = ici

            AddHandler CType(ctl2, ImgControl).DeleteEvent, AddressOf OnDelControl
            CType(ctl2, ImgControl).InitFont()
            ctl2.Location = New Point(150, 120)
            Me.Panel1.Controls.Add(ctl2)
            pb.WireControl(ctl2)
            ctl2.BringToFront()

        End If

        Me.Panel1.Controls.Add(ctl)

        pb.WireControl(ctl)

        ctl.BringToFront()

        'btnSave.Enabled = True
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If pb.CurrentSelectedControl Is Nothing Then
            msg.Show("请选中需要删除的数据图元")
            Exit Sub
        End If

        If Panel1.Controls.Contains(pb.CurrentSelectedControl) Then
            Panel1.Controls.Remove(pb.CurrentSelectedControl)
            'pb.CurrentSelectedControl.Dispose()
            pb.HideHandles()
            ''btnSave.Enabled = True
        End If

    End Sub

    Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
        TreeView1.Visible = Not TreeView1.Visible
        Me.btnOfflineData.Visible = TreeView1.Visible
    End Sub

    Private Sub lstControls_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.btnAdd.PerformClick()
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim tn As TreeNode
        tn = Me.TreeView1.SelectedNode

        If tn Is Nothing Then
            Exit Sub
        End If

        If TypeOf tn.Tag Is RefTypeItem Then
            Me.btnAdd.Enabled = False
        Else
            Me.btnAdd.Enabled = True
        End If

        ''add for bssh begin
        'If String.Compare(tn.Name, "原有系统") = 0 Then
        '    Me.btnAdd.Enabled = False
        'Else
        '    Me.btnAdd.Enabled = True
        'End If
        ''add for bssh endi
    End Sub

    Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick
        Dim tn As TreeNode
        tn = Me.TreeView1.SelectedNode

        If tn Is Nothing Then
            Exit Sub
        End If

        Me.btnAdd.PerformClick()
    End Sub


    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.X > 0 AndAlso e.X < 10 AndAlso e.Y > 2 Then
            Me.TreeView1.Visible = True
        ElseIf e.X > Me.TreeView1.Width AndAlso e.X < Me.TreeView1.Width + 20 AndAlso e.Y > 2 Then
            If _justLeaveTree Then
                Me.TreeView1.Visible = False
            Else
                _justLeaveTree = False
            End If
        End If
        Me.btnOfflineData.Visible = TreeView1.Visible
    End Sub

    Private _justLeaveTree As Boolean = False

    Private Sub TreeView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseMove
        '_justLeaveTree = True
    End Sub

    Private Sub ImageControlConfigForm_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        If Not _bLoad Then
            Exit Sub
        End If

        Dim iWidth As Integer = Me.PictureBox1.Width
        Dim iHeight As Integer = Me.PictureBox1.Height

        If iWidth * iHeight = 0 Then
            Exit Sub
        End If

        For Each ctl As Control In Me.Panel1.Controls
            If ctl.GetType() Is GetType(ImgControl) Then
                Dim item As ImgControlItem = CType(ctl, ImgControl).ici
                ''只变换位置坐标，大小保持不变
                ctl.Location = New Point((item.cLeft / 1024.0) * iWidth, (item.cTop / 768.0) * iHeight)
                'ctl.Size = New Size((item.cWidth / 1024.0) * iWidth, (item.cHeight / 768.0) * iHeight)
                ctl.Size = New Size(item.cWidth, item.cHeight)
            End If
        Next

    End Sub

    Private Sub btnOfflineData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOfflineData.Click
        Dim gp As System.Security.Principal.GenericPrincipal
        gp = My.User.CurrentPrincipal
        If gp.IsInRole(RoleClass.GetRolename(RoleType.Admin)) Then

        End If

        If Me.btnOfflineData.Text.Equals("离线数据") Then
            Me.btnOfflineData.Text = "在线数据"
            _isOfflineMode = True
            LoadCtrlTypes_Offline()
        Else
            Me.btnOfflineData.Text = "离线数据"
            _isOfflineMode = False
            LoadCtrlTypes_Online()
        End If
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click

        Me.Panel1.Controls.Clear()

        If _isOfflineMode = True Then
            UpdateLabelText_Offline()
        Else
            UpdateLabelText_Online()
        End If

        loadImage()
        LoadControls()
        Me.PictureBox1.SendToBack()

    End Sub

    Private Sub UpdateLabelText_Offline()
        Dim strSql As String = "Update tbccImageControls Set " & _
                                "TitleMsg='{5}' " & _
                                "Where ProjectID='{0}' and Netid='{1}' and Refid='{2}' and DataType='{3}' and PortNo='{4}'"
        Dim rts As New RefTypeService(_pi)

        For Each rti As RefTypeItem In rts.GeRefGroupList() '增加冷库名称
            Dim ipts As New IOPortTypeService(rti)

            For Each ipi As IOPortTypeItem In ipts.GetAiGroupList()
                SqlHelper.ExecuteNonQuery(String.Format(strSql, ipi.ProjectID, ipi.NetId, ipi.RefId, ipi.DataType, ipi.PortNo, ipi.PortName & ":"), Nothing)
            Next
        Next
    End Sub

    Private Sub UpdateLabelText_Online()
        Dim strSql As String = "Update TbccAiInfo Set " & _
                                "TitleMsg='{5}' " & _
                                "Where ProjectID='{0}' and Netid='{1}' and Refid='{2}' and DataType='{3}' and PortNo='{4}'"
        Dim rts As New RefTypeService(_pi)

        For Each rti As RefTypeItem In rts.GeRefGroupList() '增加冷库名称
            Dim ats As New AiPortTypeService(rti)

            For Each apti As AiPortTypeItem In ats.GetAiGroupList()
                SqlHelper.ExecuteNonQuery(String.Format(strSql, apti.ProjectID, apti.NetId, apti.RefId, apti.DataType, apti.PortNo, apti.PortName & ":"), Nothing)
            Next
        Next
    End Sub

End Class